home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.12 Dec 91 / MBOB Source / Utils.c < prev   
Encoding:
C/C++ Source or Header  |  1991-09-17  |  6.1 KB  |  410 lines  |  [TEXT/MSWD]

  1. extern    gEOL[];
  2. extern    gSP[];
  3. extern    gTAB[];
  4.  
  5.  
  6. C2Pstr(src, dst)
  7.     unsigned char src[], dst[];
  8.  
  9. /* C 2 Pascal String converts a  C string into a pascal string */
  10.  
  11. {
  12.     int    i = 0;
  13.     unsigned char j;
  14.  
  15.     /* put length at start of destination string */
  16.  
  17.     j = (unsigned char) strlen(src);
  18.     dst[0] = j;
  19.  
  20.     /* copy chars until delimiter encountered */
  21.  
  22.     while (src[i] != *gEOL)
  23.     {
  24.         dst[i + 1] = src[i];
  25.         i++;
  26.     }
  27. }
  28.  
  29.  
  30.  
  31. i2a(n, s)
  32.     int        n;
  33.     char    s[];
  34.     
  35. /*
  36.     convert n to character string in array s.  Taken from pp.60 k & r.
  37. */
  38.  
  39. {
  40.     int i, sign;
  41.     
  42.     if((sign = n) < 0)        /* record sign */
  43.         n = -n;                /* make n positive */
  44.     i = 0;
  45.     
  46.     /* generate digits in reverse order */
  47.     
  48.     do
  49.     {
  50.         s[i++] = n % 10 + '0';        /* get next digit */
  51.     } while ((n /= 10) > 0);        /* delete it */
  52.     
  53.     if (sign < 0)
  54.         s[i++] = '-';
  55.     s[i] = *gEOL;
  56.     reverse(s);
  57. }
  58.  
  59.  
  60.  
  61. reverse(s)
  62.     char s[];
  63.     
  64. /*
  65.     reverse string s in place
  66. */
  67.  
  68. {
  69.     int c, i, j;
  70.     
  71.     for (i = 0, j = strlen(s) - 1; i < j; i++, j--)
  72.     {
  73.         c = s[i];
  74.         s[i] = s[j];
  75.         s[j] = c;
  76.     }
  77. }
  78.  
  79.  
  80.  
  81.  
  82. double a2f(s)
  83.     char s[];
  84.     
  85. /* 
  86.         convert a string s to double.  lifted from k & r pp. 69 
  87. */
  88.  
  89. {
  90.     double    val, power;
  91.     int        i, sign;
  92.     
  93.     for (i = 0; s[i] == *gSP || s[i] == '\n' || s[i] == *gTAB; i++)
  94.         ;    /* blow off white spaces */
  95.         
  96.     sign = 1;
  97.     
  98.     if (s[i] == '+' || s[i] == '-')    /* sign */
  99.         sign = (s[i++] == '+') ? 1 : -1;
  100.     for (val = 0; s[i] >= '0' && s[i] <= '9'; i++)
  101.         val = 10 * val + s[i] - '0';
  102.     if (s[i] == '.')
  103.         i++;
  104.     for (power = 1; s[i] >= '0' && s[i] <= '9'; i++)
  105.     {
  106.         val = 10 * val + s[i] - '0';
  107.         power *= 10;
  108.     }
  109.     return(sign * val / power);
  110. }
  111.  
  112.  
  113.  
  114.  
  115. P2Cstr(source, dest)
  116.  
  117. /*    This function converts a Pascal string into a C string */
  118.  
  119. char    source[], dest[];
  120.     
  121. {
  122.     char ch;
  123.     int i, j;
  124.     
  125.     ch = source[0];    /* get string length; from 1st ele of pascal string */
  126.     j = (int) ch;    /* convert to integer */
  127.     
  128.     
  129.     for (i = 0; i < j; i++)
  130.         dest[i] = source[i + 1];
  131.         
  132.     while (i < 256)
  133.     {
  134.         dest[i] = '\0';        /* tack on delimiter to end of string */
  135.         i++;
  136.     }
  137.     
  138.     return(j);            /* ret it length */
  139.     
  140. }
  141.  
  142.  
  143.  
  144.  
  145. pStrCpy(src, dst)
  146.     char    *src,
  147.             *dst;
  148.             
  149. /* pascal string copy function */
  150.  
  151. {
  152.     int    i,
  153.         length;
  154.         
  155.     length = *src;
  156.     
  157.     for (i = 0; i <= length + 1; i++)
  158.     {
  159.         *dst = *src;
  160.         src++; dst++;
  161.     }
  162. }
  163.  
  164.  
  165.  
  166.  
  167. ZeroStruct(a, b)
  168.     Ptr    a;    /* ptr to structure */
  169.     int    b;    /* iteration index */
  170.  
  171. /*
  172.     Zero Structure does just that with the two parameters passed to it.  The
  173.   first, is the ptr to the structure and the second is the loop index.
  174. */
  175.  
  176. {
  177.     int i;
  178.  
  179.     for(i = 0; i < b; i++)
  180.         ((unsigned char *) a) [i] = 0;
  181.  
  182. }
  183.  
  184.  
  185.  
  186.  
  187. a2i(s)
  188.     char s[];
  189.     
  190. /*
  191.     convert s to integer.  Taken from K & R pp. #58.
  192. */
  193.  
  194. {
  195.     int    i, n, sign;
  196.     
  197.     for (i = 0; s[i] == ' ' || s[i] == '\n' || s[i] == '\t'; i++)
  198.         ;    /* skip white spaces */
  199.         
  200.     sign = 1;
  201.     if (s[i] == '+' || s[i] =='-')    /* sign */
  202.         sign = (s[i++] == '+') ? 1 : -1;
  203.     for (n = 0; s[i] >= '0' && s[i] <= '9'; i++)
  204.         n = 10 * n + s[i] - '0';
  205.     return(sign * n);
  206. }
  207.  
  208.  
  209.  
  210.  
  211. isItADigit(cPtr)
  212.     char    *cPtr;
  213.     
  214. /* 
  215.         checks pascal strings for non-numeric characters and returns true 
  216.     value if no invalid characters are detected.
  217. */
  218.  
  219. {
  220.     char    ch;
  221.     int        i,
  222.             length = 0;
  223.             
  224.     length = *cPtr;        /* extract length of pascal string */
  225.     
  226.     if (length == 0)
  227.         return(FALSE);        /* zero length string */
  228.     
  229.     for (i = 1; i <= length; i++)
  230.     {
  231.         ch = *(cPtr + i);
  232.         if ((ch >= '0') && (ch <= '9'))
  233.             ;
  234.         else
  235.             return(FALSE);
  236.     }
  237.     return(TRUE);
  238. }
  239.  
  240.  
  241.  
  242.  
  243. cmpStruct(strct1, strct2, length)
  244.     unsigned char    *strct1,
  245.                     *strct2;
  246.     int                length;
  247.     
  248. /*
  249.         compares structures returning a true if they are equal or a false
  250.     if they are not.
  251. */
  252.  
  253. {
  254.     int    i;
  255.     
  256.     for (i = 0; i < length; i++)
  257.     {
  258.         if (*strct1 == *strct2)
  259.         {
  260.             strct1++;
  261.             strct2++;
  262.         }
  263.         else
  264.             return(FALSE);        /* not equal */
  265.     }
  266.     return(TRUE);                /* equal */
  267. }
  268.  
  269.  
  270.  
  271.  
  272. Adigit(c)
  273.     char *c;
  274.     
  275. /*
  276.         returns true | false.  This function works for integers and the
  277.     integer portion of a floating point number.
  278. */
  279.  
  280. {
  281.     /* look at first char to see if it is +ive | -ive */
  282.     
  283.     if (*c == '-' || *c =='+')
  284.     {
  285.         c++;
  286.         
  287.         /* look at next char to determine if digit */
  288.         
  289.         if (*c >= '0' && *c <= '9')
  290.             return(TRUE);
  291.         else
  292.             return(FALSE);
  293.     }
  294.     
  295.     /* otherwise look only at first char */
  296.     if (*c >= '0' && *c <= '9')
  297.         return(TRUE);
  298.     else
  299.         return(FALSE);
  300. }
  301.  
  302.  
  303.  
  304.  
  305. f2a(aNumber, aStr, precision)
  306.     float    aNumber;
  307.     char    *aStr;
  308.     int        precision;
  309.     
  310. /*
  311.         converts a float number to an ascii string with n digits of precision
  312.     to the right of the decimal point;ie, a precision of zero generates an
  313.     long and a precistion of two generates an integer with a mantissa of two
  314.     digits.
  315.     
  316.         Note:
  317.             The variable The variable aStr must be cleared before entry to this routine or the funct
  318.             returns strange results.
  319. */
  320.  
  321. {
  322.     float    sign;
  323.     int     i = 0, l;
  324.     long    tmp = 0;
  325.     
  326.     if (precision < 0)                    /* range check */
  327.         precision = 0;
  328.         
  329.     if ((sign = aNumber) < 0)            /* record sign */
  330.     {
  331.         aStr[i++] = '-';
  332.         aNumber = - aNumber;            /* make n positive */
  333.     }
  334.     
  335.     /* generate long portion */
  336.     
  337.     do
  338.     {    /* generate digits in reverse order */
  339.         aStr[i++] = (long) aNumber % 10 + '0';    /* get next digit */
  340.         aNumber /= 10;
  341.         tmp = aNumber;
  342.     } while ((tmp) > 0);                        /* delete it */
  343.     
  344.     if (sign < 0)                                /* reverse long portion */
  345.         reverse(aStr + 1);
  346.     else
  347.         reverse(aStr);
  348.     
  349.     /* create mantissa */
  350.     
  351.     if (precision != 0)
  352.     {
  353.         aStr[i] = '.';
  354.         i++;
  355.         
  356.         /* make mantissa a long with the precision of n decimal places */
  357.         
  358.         aNumber = sign;
  359.         tmp = aNumber;                                /* trunc mantissa */
  360.         aNumber -= tmp;                                /* restore only mantissa */
  361.         
  362.                 
  363.         if (aNumber < 0)                            /* make +ive */
  364.             aNumber = - aNumber;
  365.             
  366.         for (l = 1; l <= precision; l++)
  367.         {
  368.             aNumber *= 10;
  369.         
  370.             aStr[i++] = (long) aNumber % 10 + '0';    /* get next digit */
  371.             tmp = aNumber;
  372.             aNumber -= tmp;                            /* rotate left one place */
  373.         }
  374.     }
  375.     
  376.     aStr[i] = *gEOL;                                /* tack on delimiter */
  377.  
  378. }
  379.  
  380.  
  381.  
  382.  
  383. cmpss(s1, s2)
  384.     char *s1, *s2;
  385.     
  386. /*
  387.         compares for occurance of substring s1 in s2 and returns either a
  388.     zero value of match found, or a minus one value if no match found.
  389. */
  390.  
  391. {
  392.     int    i,
  393.         s1Length = 0,
  394.         s2Length = 0;
  395.         
  396.     s1Length = strlen(s1);
  397.     s2Length = strlen(s2);
  398.         
  399.     /* compare for substring inc'ing s2 and only chking for s1 length chars */
  400.         
  401.     for (i = 0; i < s2Length; i++)
  402.     {
  403.         if (strncmp(s1, s2 + i, s1Length) == 0)
  404.             return(0);
  405.     }
  406.         
  407.     return(-1);
  408. }
  409.  
  410.